﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Script.Serialization;

namespace Web.UI
{
    [Obsolete("此类已经移动到MAF.Web中，不要再更新此类")]
    public class UiHelper
    {
        /// <summary>
        /// 执行非查询方法(增，改，删)
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="method"></param>
        public static string ExecuteNonQuery(string typeName, string method)
        {
            // 获取类型
            Type objType = GetObjectType(typeName);
            // 创建对象
            object objInstance = Activator.CreateInstance(objType);
            // 调用方法
            object o = objType.InvokeMember(method, BindingFlags.InvokeMethod, null, objInstance, null);
            if (null == o)
                return string.Empty;
            else
                return o.ToString();
        }

        /// <summary>
        /// 执行查询方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="typeName"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        public static string Query(string typeName, string method)
        {
            // 获取类型
            Type objType = GetObjectType(typeName);
            // 创建对象
            object objInstance = Activator.CreateInstance(objType);
            // 调用方法
            object t = objType.InvokeMember(method, BindingFlags.InvokeMethod, null, objInstance, null);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(t);
        }

        /// <summary>
        /// 获取类型
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        private static Type GetObjectType(string typeName)
        {
            return Type.GetType(string.Format("Web.UI.{0}UI", typeName), true, true);
        }
    }
}